﻿using System;
using System.Windows.Forms;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;

namespace TSPquestEDITOR
{
    public partial class basic_info : Form
    {
        public void load_quest_strings()
        {
            SqlConnection qCon = new SqlConnection(connectionString);
            qCon.Open();

            bgw.ReportProgress(52, "READING QUEST STRINGS");

            //Lets Load Quest Name String
            //Select Quest Name
            SqlCommand quest_name = qCon.CreateCommand();
            quest_name.CommandText = "SELECT value FROM dbo.StringResource WHERE code = '" + quest_name_id_val + "'";
            string quest_name_res = ((string)quest_name.ExecuteScalar());
            quest_name_val = quest_name_res;

            //Lets Load NPC Related Strings
            //We should start by checking that a start npc_id was provided
            if (quest_start_npc_id_val != 0)
            {
                //Since he was provided let's load his information

                //Select Start Title
                SqlCommand quest_start_giver_title = qCon.CreateCommand();
                quest_start_giver_title.CommandText = "SELECT value FROM dbo.StringResource WHERE code = '" + quest_start_npc_title_id_val + "'";
                string quest_start_giver_title_res = ((string)quest_start_giver_title.ExecuteScalar());
                quest_start_npc_title_val = quest_start_giver_title_res;

                //Select Start NPC Name
                SqlCommand quest_start_giver_name = qCon.CreateCommand();
                quest_start_giver_name.CommandText = "SELECT SRES.value FROM dbo.NPCResource NRES INNER JOIN dbo.StringResource SRES ON SRES.code = NRES.name_text_id WHERE NRES.id = '" + quest_start_npc_id_val + "'";
                string quest_start_giver_name_res = ((string)quest_start_giver_name.ExecuteScalar());
                quest_start_npc_name_val = quest_start_giver_name_res;
            }
            else
            {
                //The quest_start_npc_id == 0
                quest_start_npc_title_val = "";
                quest_start_npc_name_val = "";
                quest_start_npc_x_val = 0;
                quest_start_npc_y_val = 0;
                quest_start_npc_lua_val = "";
            }
            //Now that Start NPC 1 info has been loaded lets check for a NPC 2
            if (quest_start_npc_2_id_val != 0)
            {
                //Since he was provided let's load his information

                //Select Start Title
                SqlCommand quest_start_giver_title_2 = qCon.CreateCommand();
                quest_start_giver_title_2.CommandText = "SELECT SRES.value FROM dbo.NPCResource NRES INNER JOIN dbo.StringResource SRES ON SRES.code = NRES.text_id WHERE NRES.id = '" + quest_start_npc_2_id_val + "'";
                string quest_start_giver_title_res_2 = ((string)quest_start_giver_title_2.ExecuteScalar());
                quest_start_npc_2_title_val = quest_start_giver_title_res_2;

                //Select Start NPC Name
                SqlCommand quest_start_giver_name_2 = qCon.CreateCommand();
                quest_start_giver_name_2.CommandText = "SELECT SRES.value FROM dbo.NPCResource NRES INNER JOIN dbo.StringResource SRES ON SRES.code = NRES.name_text_id WHERE NRES.id = '" + quest_start_npc_2_id_val + "'";
                string quest_start_giver_name_res_2 = ((string)quest_start_giver_name_2.ExecuteScalar());
                quest_start_npc_2_name_val = quest_start_giver_name_res_2;
            }
            //Now that Start NPC 2 info has been loaded lets check for a NPC 3
            if (quest_start_npc_3_id_val != 0)
            {
                //Since he was provided lets load his information

                //Select Start Title
                SqlCommand quest_start_giver_title_3 = qCon.CreateCommand();
                quest_start_giver_title_3.CommandText = "SELECT SRES.value FROM dbo.NPCResource NRES INNER JOIN dbo.StringResource SRES ON SRES.code = NRES.text_id WHERE NRES.id = '" + quest_start_npc_3_id_val + "'";
                string quest_start_giver_title_3_res = ((string)quest_start_giver_title_3.ExecuteScalar());
                quest_start_npc_3_title_val = quest_start_giver_title_3_res;

                //Select Start NPC Name
                SqlCommand quest_start_giver_name_3 = qCon.CreateCommand();
                quest_start_giver_name_3.CommandText = "SELECT SRES.value FROM dbo.NPCResource NRES INNER JOIN dbo.StringResource SRES ON SRES.code = NRES.name_text_id WHERE NRES.id = '" + quest_start_npc_3_id_val + "'";
                string quest_start_giver_name_3_res = ((string)quest_start_giver_name_3.ExecuteScalar());
                quest_start_npc_3_name_val = quest_start_giver_name_3_res;
            }
            if (quest_end_npc_id_val != 0)
            {
                //Select End Title
                SqlCommand quest_end_giver_title = qCon.CreateCommand();
                quest_end_giver_title.CommandText = "SELECT SRES.value FROM dbo.NPCResource NRES INNER JOIN dbo.StringResource SRES ON SRES.code = NRES.text_id WHERE NRES.id = '" + quest_end_npc_id_val + "'";
                string quest_end_giver_title_res = ((string)quest_end_giver_title.ExecuteScalar());
                quest_end_npc_title_val = quest_end_giver_title_res;

                //Select End NPC Name
                SqlCommand quest_end_giver_name = qCon.CreateCommand();
                quest_end_giver_name.CommandText = "SELECT SRES.value FROM dbo.NPCResource NRES INNER JOIN dbo.StringResource SRES ON SRES.code = NRES.name_text_id WHERE NRES.id = '" + quest_end_npc_id_val + "'";
                string quest_end_giver_name_res = ((string)quest_end_giver_name.ExecuteScalar());
                quest_end_npc_name_val = quest_end_giver_name_res;
            }
            else
            {
                quest_end_npc_title_val = "";
                quest_end_npc_name_val = "";
                quest_end_npc_x_val = 0;
                quest_end_npc_y_val = 0;
                quest_end_npc_lua_val = "";
            }
            if (quest_def_rew_id_val != 0)
            {
                //Depending on Quest Type Load Strings
                SqlCommand quest_def_rew_name = qCon.CreateCommand();
                quest_def_rew_name.CommandText = "SELECT SRES.value FROM dbo.ItemResource IRES INNER JOIN dbo.StringResource SRES ON SRES.code = IRES.name_id WHERE IRES.id = '" + quest_def_rew_id_val + "'";
                string quest_def_rew_name_res = ((string)quest_def_rew_name.ExecuteScalar());
                quest_def_rew_name_val = quest_def_rew_name_res;

            }

            bgw.ReportProgress(55, "READING QUEST STRINGS");
        }

        public void load_start_desc_id()
        {
            SqlConnection qCon = new SqlConnection(connectionString);

            SqlCommand questlink_sel = new SqlCommand(
            "SELECT * FROM dbo.QuestLinkResource WHERE npc_id = '" + quest_start_npc_id_val + "' AND quest_id = '" + fin_qID + "'",
            qCon);

            qCon.Open();

            SqlDataReader questlink_reader = questlink_sel.ExecuteReader();

            if (questlink_reader.HasRows)
            {
                while (questlink_reader.Read())
                {
                    quest_start_desc_id_val = questlink_reader.GetInt32(5);
                    quest_prog_desc_id_val = questlink_reader.GetInt32(6);
                }

            }
            questlink_reader.Close();
            bgw.ReportProgress(58, "READING QUEST (START) DESCRIPTION INFO");
        }

        public void load_end_desc_id()
        {

            SqlConnection qCon = new SqlConnection(connectionString);

            SqlCommand questlink_sel = new SqlCommand(
            "SELECT * FROM dbo.QuestLinkResource WHERE npc_id = '" + quest_end_npc_id_val + "' AND quest_id = '" + fin_qID + "'",
            qCon);

            qCon.Open();

            SqlDataReader questlink_reader = questlink_sel.ExecuteReader();

            if (questlink_reader.HasRows)
            {
                while (questlink_reader.Read())
                {
                    quest_end_desc_id_val = questlink_reader.GetInt32(7);
                }

            }
            questlink_reader.Close();
            bgw.ReportProgress(75, "READING QUEST (END) DESCRIPTION INFO");
        }

        public void translate_strings()
        {
            SqlConnection qCon = new SqlConnection(connectionString);

            qCon.Open();

            //Select Quest Starting Description
            SqlCommand quest_start_desc = qCon.CreateCommand();
            quest_start_desc.CommandText = "SELECT value FROM dbo.StringResource WHERE code = '" + quest_start_desc_id_val + "'";
            string quest_start_desc_res = ((string)quest_start_desc.ExecuteScalar());
            quest_start_desc_val = quest_start_desc_res;

            //Select Quest In-Progress Description
            SqlCommand quest_progress_desc = qCon.CreateCommand();
            quest_progress_desc.CommandText = "SELECT value FROM dbo.StringResource WHERE code = '" + quest_prog_desc_id_val + "'";
            string quest_progress_desc_res = ((string)quest_progress_desc.ExecuteScalar());
            quest_prog_desc_val = quest_progress_desc_res;

            //Select Quest Ending Description
            SqlCommand quest_end_desc = qCon.CreateCommand();
            quest_end_desc.CommandText = "SELECT value FROM dbo.StringResource WHERE code = '" + quest_end_desc_id_val + "'";
            string quest_end_desc_res = ((string)quest_end_desc.ExecuteScalar());
            quest_end_desc_val = quest_end_desc_res;

            qCon.Close();
        }
    }
}
